package com.aishua.fire.controller;

import java.util.List;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.aishua.fire.common.JsonArrayResult;
import com.aishua.fire.common.JsonObjectResult;
import com.aishua.fire.common.ValidateUtil;
import com.aishua.fire.common.anno.ModuleService;
import com.aishua.fire.common.constant.BigMenuConstants;
import com.aishua.fire.common.constant.ModuleConstants;
import com.aishua.fire.common.exp.ValidateException;
import com.aishua.fire.entity.statutestudy.AddStatuteStudyInfo;
import com.aishua.fire.entity.statutestudy.StatuteStudyList;
import com.aishua.fire.service.StatuteStudyService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(value = "应急法规学习管理",tags = {"应急法规学习模块接口"})
@RestController
@Slf4j
public class StatuteStudyController
{

	@Autowired
	private StatuteStudyService studyService;
	
	@Autowired
	private ValidateUtil validateUtil;
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="应急法规学习_新增应急法规学习信息")
	@PostMapping(value = {"/statutestudy"} )
	public ResponseEntity<JsonObjectResult> add(@RequestBody @Valid AddStatuteStudyInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急法规学习_新增应急法规学习目录信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = studyService.save(info);
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="应急法规学习_查询应急法规学习列表")
	@GetMapping(value = {"/statutestudy"} )
	public ResponseEntity<JsonArrayResult> list(@RequestParam(name = "s_id") String s_id,@RequestParam(name = "title") String title,
			@RequestParam(name = "pageNumber") int pageNumber, @RequestParam(name = "pageSize") int pageSize)
	{
		log.info("组织结构管理_查询当前组织结构下所有员工:{}",s_id);
		
		List<StatuteStudyList> books = studyService.list(s_id, title, pageNumber, pageSize);
		int total = studyService.listTotal(s_id, title);
		log.info("查询总条数：{}", total);
		JsonArrayResult r = JsonArrayResult.makeSuccessResult(books, total);

		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="应急法规学习_修改应急法规学习信息")
	@PutMapping(value = {"/statutestudy"} )
	public ResponseEntity<JsonObjectResult> update(@RequestBody @Valid AddStatuteStudyInfo info, BindingResult result) throws ValidateException
	{
		log.info("应急法规学习_修改应急法规学习信息:{}",info);
		validateUtil.respErrorMsg(result);//非空验证
		boolean flag = studyService.update(info);
		JsonObjectResult r = null;
		if(flag)
		{
			r = JsonObjectResult.makeSuccessResult(null);
		}
		else
		{
			r = JsonObjectResult.makeErrorResult("名称重复");
		}
		return ResponseEntity.ok(r);
    }
	
	@SuppressWarnings("rawtypes")
	@ModuleService(totalModule=BigMenuConstants.organizationManager,moduleName=ModuleConstants.organizationManager)
	@ApiOperation(value="应急法规学习_删除应急法规学习信息")
	@DeleteMapping(value = {"/statutestudy"} )
	public ResponseEntity<JsonObjectResult> delete(@RequestBody AddStatuteStudyInfo info) 
	{
		log.info("应急法规学习_删除应急法规学习信息:{}",info.getId());
		studyService.delete(info.getId());
		
		JsonObjectResult r = JsonObjectResult.makeSuccessResult(null);
		return ResponseEntity.ok(r);
    }
}
